package com.lap.auth.adapter.driving.persistence.role;

import com.lap.auth.adapter.driving.persistence.role.po.RoleDataScopePo;
import com.lap.auth.adapter.driving.persistence.role.po.RolePo;
import com.lap.auth.domain.role.Role;
import com.lap.auth.domain.role.valueobject.DataScopeType;
import com.lap.auth.domain.role.valueobject.RoleDataScope;
import com.lap.auth.domain.shared.tools.BaseConverter;
import com.lap.framework.tools.EnumUtil;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import org.apache.commons.collections4.CollectionUtils;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;

@Mapper(componentModel = "spring")
public interface RoleConverter extends BaseConverter {

  @Mapping(target = "event", ignore = true)
  Role toDomain(RolePo rolePo);

  List<Role> toDomains(List<RolePo> list);

  RolePo toPo(Role role);

  @Mapping(target = "scopeType",source = "scopeType")
  @Mapping(target = "deptSet",source = "deptIds")
  RoleDataScope toScopeDomain(RoleDataScopePo roleDataScopePo);

  List<RoleDataScope> toScopeDomains(List<RoleDataScopePo> list);

  @Mapping(target = "deptIds", source = "deptSet")
  @Mapping(target = "scopeType", source = "scopeType")
  RoleDataScopePo toDataScopePo(RoleDataScope roleDataScope);

  default String setToStr(Set<Integer> set) {
    if (CollectionUtils.isEmpty(set)) return "";
    else return set.stream().map(String::valueOf).collect(Collectors.joining(","));
  }

  default Integer scopeTypeToInt(DataScopeType scopeType) {
    return scopeType.getCode();
  }

  default DataScopeType codeToScopeType(Integer code) {
    return EnumUtil.fromCode(DataScopeType.class, code);
  }
}
